<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 人物控制器
 * @author wb_wenjie.wuwj <wb_wenjie.wuwj@alibaba-inc.com>
 * @copyright 2013-2-21
 */

class Person extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		
		// 后台登录验证
		$this->check_login();
		
		// 验证权限
		$this->verify_purview(PURVIEW_PERSON);
		
		$this->load->model('person_m');
		
		// 加载人物的语言包
		$this->load->language('person');
		
		$this->validate_data = array(
				array('field' => 'personname', 'label' => 'lang:person_name', 'rules' => 'trim|required|max_length[10]|xss_clean'),
				array('field' => 'gender', 'label' => 'lang:person_gender', 'rules' => 'trim|required|is_natural'),
				array('field' => 'race', 'label' => 'lang:person_race', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'birthday', 'label' => 'lang:person_birthday', 'rules' => 'trim|max_length[10]'),
				array('field' => 'path', 'label' => 'lang:person_photo', 'rules' => 'trim|max_length[200]'),
				array('field' => 'content', 'label' => 'lang:person_introduction', 'rules' => 'trim|required'),
				array('field' => 'province', 'label' => 'lang:person_address', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'city', 'label' => 'lang:person_address', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'company', 'label' => 'lang:person_company', 'rules' => 'trim|required|max_length[30]'),
				array('field' => 'position', 'label' => 'lang:person_position', 'rules' => 'trim|required|max_length[15]'),
				array('field' => 'keyword', 'label' => 'lang:com_keyword', 'rules' => 'trim|max_length[30]'),
				array('field' => 'classid', 'label' => 'lang:com_column_name', 'rules' => 'trim|required|is_natural_no_zero'),
				array('field' => 'releasename', 'label' => 'lang:com_releasename', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'remark', 'label' => 'lang:com_remark', 'rules' => 'trim|max_length[20]'),
				
				array('field' => 'email', 'label' => 'lang:person_email' ,'rules' => 'trim|valid_email'),
				array('field' => 'postcode', 'label' => 'lang:person_postcode' ,'rules' => 'trim|numeric|exact_length[6]'),
				array('field' => 'mobile', 'label' => 'lang:person_mobile' ,'rules' => 'trim|numeric|min_length[11]|max_length[14]'),
				array('field' => 'qq', 'label' => 'lang:person_qq' ,'rules' => 'trim|numeric|max_length[10]'),
				);
	}
	
	function index($page = '')
	{
		$this->manage($page);
	}
	
	/**
	 * 显示人物列表
	 * @param int $page 显示的页码
	 */
	function manage($page = '')
	{
		// 获取要显示的页数
		$page           = empty($page) ? 1 : abs(intval($page));
	
		// 组织分页信息
		$config['base_url']		= base_url('person/manage');
		$config['total_rows']	= $this->person_m->get_person_count();
	
		$page_data = $this->get_page($config);
		$data['page'] 	= $page_data['page'];
		$order 			= 'istop desc,isrecommend desc,id desc';
	
		// 查询人物
		$person      = $this->person_m->show_person($page_data['limit'], $order);
	
		// 人物信息列表数组
		$i = 1;
		foreach ($person as $key => $row)
		{
			$person[$key]['ordernum'] 	= ($page - 1) * $page_data['per_page'] + $i;
			$person[$key]['createtime']	= date(DATE_FORMAT,$row['createtime']);
			$person[$key]['updatetime']	= date(DATE_FORMAT,$row['updatetime']);
			$person[$key]['isactive'] 	= $row['isactive'] == 0 ? $this->lang->line('com_sign_inactive') : $this->lang->line('com_sign_active');
			$person[$key]['isverify'] 	= $row['isverify'] == 0 ? $this->lang->line('com_sign_not_verify') : $this->lang->line('com_sign_verify');
			$person[$key]['isrecommend'] = $row['isrecommend'] == 1 ? $this->lang->line('com_sign_recommend') : '';
			$person[$key]['istop'] 		= $row['istop'] == 1 ? $this->lang->line('com_sign_top') : '';
			$person[$key]['path'] 		= $this->get_uploadfile_path($row['path']);
			$i++;
		}
	
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('person_title_list');
		$data['page_label']		= $this->lang->line('person_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['person'] 	= $person;
		$this->load->view('person_list',$data);
	}
	
	/**
	 * 显示人物添加页面
	 */
	function add()
	{
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('person_title_add');
		$data['page_label']		= $this->lang->line('person_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['loginname'] 		= $this->loginname;
		$data['class'] 			= $this->get_column_as_array();
		$this->load->view('person_add', $data);
	}
	
	/**
	 * 添加人物
	 */
	function add_action()
	{
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('person/add'), $validate, 'goback');
		}
	
		$person = $this->input->post();
		$content = array();
		$photo = array();
		if ($person && isset($person['s_person_add']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($person['classid']))
			{
				$this->_show_message(base_url('person/add'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
	
			// 组建照片数组
			if (!empty($person['path']))
			{
				$photo['personname'] = $person['personname'];
				$photo['path'] = $person['path'];
				$photo['phototype'] = 0;
			}
			$person['birthday'] = strtotime($person['birthday']);
			$person['updatetime'] = $person['createtime'] = time();
			$content['content'] = $person['content'];
			$content['keyword'] = $person['keyword'];
	
			// 清除数组中无用的数据，添加相关信息
			unset($person['s_person_add']);
			unset($person['content']);
			unset($person['keyword']);
			unset($person['path']);
	
			// 添加
			$result = $this->person_m->insert_person($person, $content, $photo);
			if ($result)
			{
				$this->_show_message(base_url('person'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('person/add'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('person'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * 显示编辑人物页面
	 * @param int $id 人物id
	 */
	function edit($id)
	{
		$person_id['person.id'] = $id = intval($id);
		if (empty($person_id))
		{
			$this->_show_message(base_url('person'), $this->lang->line('person_id_unable_empty'), 'goback');
		}
	
		// 查询信息
		$person = $this->person_m->get_person($person_id);
		if (empty($person))
		{
			$this->_show_message(base_url('person'), $this->lang->line('person_does_not_exist'), 'goback');
		}
		$person = $person[0];
		$person['birthday'] = date(DATE_FORMAT_PERSON, $person['birthday']);
		$data = array (
					
				'header' => $this->header,
				'person' => $person,
				'class' => $this->get_column_as_array(),
				'loginname'	=> $this->loginname,
				'title' => $this->lang->line('person_title_edit'),
				'page_label' => $this->lang->line('person_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label'),
				'redirect' => $this->get_referer_url(),
		);
		$this->load->view('person_edit', $data);
	}
	
	/**
	 * 编辑人物
	 */
	function edit_action()
	{
		$redirect = empty($_GET['redirect']) ? base_url('person') : $_GET['redirect'];
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('person/manage'), $validate, 'goback');
		}
	
		$person = $this->input->post();
		$content = array();
		$photo = array();
		if ($person && isset($person['s_person_edit']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($person['classid']))
			{
				$this->_show_message(base_url('person/add'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
	
			// 人物id不能为空
			$id = $person['person_id'];
			if (empty($id))
			{
				$this->_show_message(base_url('person'), $this->lang->line('person_id_unable_empty'), 'goback');
			}
	
			// 更新人物照片，不做旧照片的删除
			$photo_count = $this->person_m->count_all_results(array('personid' => $id));
			if ($photo_count >= 1)
			{
				// 编辑后，照片不为空，直接更新，否则删除原来的照片及表记录
				if (!empty($person['path']))
				{
					$photo['personname'] = $person['personname'];
					$photo['path'] = $person['path'];
				}
				else
				{
					$photo['personid'] = 0;
				}
			}
			else
			{
				// 原来没有照片，编辑之后添加照片
				if (!empty($person['path']))
				{
					$photo['personid'] = $id;
					$photo['personname'] = $person['personname'];
					$photo['path'] = $person['path'];
					$this->person_m->insert_person_photo($photo);
					$photo = array();
				}
			}
	
			$person['updatetime'] = time();
			$person['birthday'] = strtotime($person['birthday']);
			$content['content'] = $person['content'];
			$content['keyword'] = $person['keyword'];
	
			// 清除数组中无用的数据，添加相关信息
			unset($person['person_id']);
			unset($person['origin_classid']);
			unset($person['s_person_edit']);
			unset($person['content']);
			unset($person['keyword']);
			unset($person['path']);
	
			// 更新
			$result = $this->person_m->update_person(array('id' => $id), $person);
			$this->person_m->update_person_introduction(array('personid' => $id), $content);
			if (!empty($photo))
			{
				$result = $this->person_m->update_person_photo(array('personid' => $id), $photo);
			}
	
			if ($result)
			{
				$this->_show_message($redirect, $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('person'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('person'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * ajax删除人物，返回json结果
	 */
	function delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:person_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return ;
		}
	
		// 获取文章ID值，并放入数组
		$id = $this->input->post('id');
		if ($this->person_m->delete_person_by_id($id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
	
	/**
	 * 人物状态设置
	 * @param int $id
	 */
	function person_state($id)
	{
		$id = (int)$id;
		$person = $this->person_m->get_person_state(array('id' => $id), '1');
		if (empty($person))
		{
			$this->_show_message(base_url('person'), $this->lang->line('person_does_not_exist'), 'goback');
		}
		$person = $person[0];
		
		// 为了便于使用统一的模板，将人物姓名添加到title为索引的数组变量中
		$person['title'] = $person['personname'];
		$data = array (
					
				'header' => $this->header,
				'state' => $person,
				'title' => $this->lang->line('person_title_state_config'),
				'model' => 'person'
		);
		$this->load->view('state_config', $data);
	}
	
	/* (non-PHPdoc)
	 * @see MY_Controller::config_state()
	 */
	function config_state()
	{
		echo parent::config_state('person_m', 'person');
		return ;
	}
}